<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/14
 * Time: 11:04
 */

namespace App\Http\Controllers\Home;

use App\Model\Address;
use App\Model\Apply;
use App\Model\BankCard;
use App\Model\Members;
use App\Model\StoresCenter;
use App\Model\MembersLevel;
use App\Model\RecordTransaction;
use App\Model\ShopMemberCart;
use App\Model\ShopOrder;
use App\Model\Users;
use App\Model\RecordWithdraw;
use Session;
use Cookie;

class CenterController extends BaseController
{
    /**
     * 安全退出
     */
    public function LogOut()
    {
        Session::put('home_user',null);
        return redirect( '/home/login/login?cookie=no' )->withErrors('退出成功！');
    }

    /**
     * 统计数量
     * @return array
     */
    public function index()
    {
        $StoresCenterModel = new StoresCenter();
        $user = session('home_user');
        $uid = $user->uid;
        //订单Model
        $ShopOrderModel = new ShopOrder();
        $orderNum = $ShopOrderModel->count_order($uid);
        $recordNum = RecordTransaction::where('uid',$uid)->count();
        //购物车Model
        $ShopMemberCart = new ShopMemberCart();
        $cartNum = $ShopMemberCart->count_cart($uid);
        //提现
        $withdrawNum = RecordWithdraw::where('uid',$uid)->count();
        //收货地址
        $addrNum = Address::where('uid',$user['uid'])->count();
        //银行卡
        $bankNum = BankCard::where('uid',$user['uid'])->count();
        //门店状态
        $StoreStatus= $StoresCenterModel->where(['uid' => $user->uid])->value("type");
         //是否为DT股东
        $DT_shareholder = Members::where('uid',$user['uid'])->value('DT_shareholder');
        $shareholder = Members::where('uid',$user['uid'])->value('shareholder');
        $isShareholder = $DT_shareholder + $shareholder;
        //是否为区域代理
        $agency = Apply::where(['uid' => $user['uid'],'status' => 1])->count();
         $MemberLevel = MembersLevel::where('id',$user['level'])->first();
         if ($user['credit6'] >= $MemberLevel['credit3'] && $user['is_credit6'] >= 1 && $user['is_credit6'] <= 3 && $user['straight_shop_isdown'] == 1){
            $instructions = '直商城达到复投标准请复投，警告三次之后就不产生任何收益。';
        } else {
            $instructions = '';
        }
        $data = [
            'order' => $orderNum,
            'cart'  => $cartNum,
            'withdraw' => $withdrawNum,
            'recordNum'=>$recordNum,
            'addrNum' => $addrNum,
            'bankNum' =>$bankNum,
            'StoreStatus' => $StoreStatus,
            'is_shareholder' =>  $isShareholder,
            'warning' => $instructions,
            'is_agency' => $agency
        ];
        if ($data) {
            return ['code' => 1,'data' => $data];
        } else {
            return ['code' => 0,'data' => '没有数据'];
        }
    }

    public function pay()
    {
        return view('Home.center.order');
    }

    public function to_lead()
    {
        $userModel = new Users();
        $user = $userModel->where("id",'<' ,100)->get()->toArray();
        foreach ($user as $k => $v) {
            $pass = CODE($v['pass'],'DECODE');
            $passer = CODE($v['passer'],'DECODE');
            $salting = RANDOM();
            $id = '';
            $level_name = '';
            if($v['qjb'] > 0){
                $level = MembersLevel::where(['conditions' =>intval($v['qjb'])])->first();
                $id = $level->id;
                $level_name = $level->level_name;
            }
            $data = [
                'name'            => $v['users'], //用户名
                'user_name'       => $v['xinming'], //真是姓名
                'password'        => ENCRYPTION($pass,$salting), //登录密码
                'password_pay'    => ENCRYPTION($passer,$salting), //支付密码
                'phone'           => empty($v['phone'])?$v['users']:$v['phone'], //支付密码
                'level'           => $id, //会员等级
                'level_name'      => $level_name, //等级名称
                'credit1'         => $v['czb'], //电子币
                'credit2'         => $v['zjine'], //奖励币
                'credit3'          => $v['futou'], //复投币
                'credit4'         => $v['integral'], //积分
                'performance1'    => $v['zquz'], //A区业绩
                'performance2'    => $v['yquz'], //B区业绩
                'is_withdraw'    => $v['txkg'], //0 禁止提现 1 允许
                'is_shift'       => $v['zzkg'],     //0 禁止转账 1 允许
                'registered'    => ($v['qjb'] > 0)?1:0,     //1 已注册 0 未注册
                'enroll_time'    => $v['riqi'],          //注册时间
                'activation_time'    => $v['riqi'],     //激活时间
                'salting'    => $salting,     //激活时间
            ];
            $rico = Members::create($data);
            dump($rico);
        }
    }

    public function test()
    {
        $userModel = new Users();
        $Members = new Members();
        $users = $userModel->where("id",'>' ,300)->where("id",'<' ,1500)->get()->toArray();
//        $users = $userModel->where("id",'<' ,300)->get()->toArray();
        foreach ($users as $k => $v) {

//            $referrer = $Members->select('uid','user_name')->where(['phone' => $v['fxr']])->first(); //分享人
//            $nodes = $Members->select('uid','user_name')->where(['phone' => $v['jdr']])->first(); //接点人
//            if ($referrer || $nodes) {
//                $pass = CODE($v['pass'],'DECODE');
//                $passer = CODE($v['passer'],'DECODE');
//                $salting = RANDOM();
//                $registered = ($v['jb'] > 0)?1:0;
                $data  = [
//                    'referrer' => $referrer->uid,
//                    'nodes' => $nodes->uid,
//                    'feferrer_name' => $referrer->user_name,
//                    'nodes_name' => $nodes->user_name,
//                    'password_pay' => ENCRYPTION($passer,$salting), //支付密码
//                    'password' => ENCRYPTION($pass,$salting), //登录密码
//                    'salting' => $salting, //登录秘钥
//                    'salting_pay' => $salting, //支付秘钥
//                    'registered' => $registered, //支付秘钥
//                    'performance1' => $v['zquz'], //A区业绩
//                    'performance2' => $v['yquz'], //B区业绩
//                    'area' => $v['qu'],//区域
//                    'original' => $v['jb'],//原始金额
//                    'performance_a' => $v['zqu'],//A区业绩 对碰奖
//                    'performance_b' => $v['yqu'],//B区业绩 对碰奖
                    'leveling' => $v['leixing'],//层碰奖设置
                ];
                $Members->where(['name' => $v['users']])->update($data);
//            }
//            dump($nodes);
        }

    }

    public function area()
    {
        $userModel = new Users();
        $Members = new Members();
        $users = $Members->where('uid' ,'<' ,300)->get()->toArray();
        foreach ($users as $k => $v) {
//            $Members->where(['uid' => $v['uid']])->update(['activation_time' => strtotime($v['activation_time'])]);
            $Members->where(['uid' => $v['uid']])->update(['enroll_time' => $v['activation_time']]);
//            if($v['nodes']){
//                $nodes = $Members->find_members(['uid' => $v['nodes']]);
//                if($v['area'] || $nodes){
//                    if($v['area'] == 'A'){
//                        $Members->where(['uid' => $nodes['uid']])->update(['a' => $v['uid']]);
//                    }
//                    if($v['area'] == 'B'){
//                        $Members->where(['uid' => $nodes['uid']])->update(['b' => $v['uid']]);
//                    }
//                }
//            }

        }


    }

    public function test1()
    {
        $Members = new Members();
        $user = $Members->where(['phone' => 15929821426])->first();
        $uid[0] = $user->uid;
        $users = $Members->where('uid' ,'>' ,$uid[0])->get()->toArray();
        $userData = $this->test2($users,$uid);

    }

    public function test2($data = array(),$uid =  array() , $pid = array())
    {
        $pid = $pid;
        $nodesid = [];
        foreach ($data as $k=>$v){
            foreach ($uid as $key => $value){
                if($v['nodes'] == $value){
                    $pid[] = $v['uid'];
                    $nodesid[] = $v['uid'];
                }
            }
        }
        if(!empty($nodesid)){
            $this->test2($data , $nodesid,$pid);
        } else {
            $Members = new Members();
            foreach ($pid as $k => $v) {
                $Members->where(['uid' => $v])->update(['isdown' => 2]);
            }
        }
    }
}